import datetime
import os
import tempfile
import traceback

import flask_restful
import requests
from flask import Blueprint, request,send_from_directory
from flask import abort
from flask import flash
from flask import redirect
from flask import render_template
from flask import session
from werkzeug.utils import secure_filename
from sqlalchemy.sql import func

from manager.app import db, agent, app, AppPage
from manager.app.device.model import Device,DeviceConfig,Action
from manager.app.event.action import doScreenShot
from config import IMAGE_PATH
api_device_bp = Blueprint('device', __name__)



@app.route("/")
def index():
    return render_template("admin.html")


@app.route("/device/manage")
def device_manage():

    return render_template("/device/manage.html", device_list=Device.load_device_list())

@app.route("/device/create", methods=['post'])
def device_create():
    device_name = request.form['device_name']
    device_type = request.form['device_type']
    device = Device()
    device.device_name = device_name
    device.device_type = device_type
    db.session.add(device)
    db.session.commit()
    return redirect("/device/manage", code=302)

@app.route("/device/<device_id>")
def device_index(device_id):
    device = Device.find_device_by_id(device_id).first()

    return render_template("device/device.html", device=device)

@app.route("/devicecfg/create",methods=['POST'])
def devicecfg_create():
    device_id = request.form['device_id']
    config_name = request.form['config_name']
    config_code = request.form['config_code']
    config_value = request.form['config_value']
    cfg = DeviceConfig()
    cfg.device_id = device_id
    cfg.config_name = config_name
    cfg.config_code = config_code
    cfg.config_value = config_value

    db.session.add(cfg)
    db.session.commit()
    return redirect("/device/%s" % device_id, code=302)



@app.route("/action/create", methods=['post'])
def action_create():
    action_name = request.form['action_name']
    action_type = request.form['action_type']
    action_command = request.form['action_command']
    action_params = request.form['action_params']

    action = Action()
    action.action_name = action_name
    action.action_type = action_type
    action.action_command = action_command
    action.action_params = action_params

    db.session.add(action)
    db.session.commit()
    return redirect("/action/manage", code=302)


@app.route("/action/manage")
def action_manage():
    return render_template("/device/action.html", action_list=Action.load_action_list())


@app.route("/deviceScreenShot/<app_id>")
def deviceScreenShot(app_id):
    obj = db.session.query(func.max(AppPage.page_id)).one()
    max_id=100
    if obj is not None and obj[0] is not None:
        max_id = int(obj[0])+1

    doScreenShot(app_id, max_id)

    page = AppPage()
    page.app_id = app_id
    page.page_id = max_id

    db.session.add(page)
    db.session.commit()
    return "success"

@app.route('/images/<app_id>/<page_id>')
def show_images(app_id,page_id):
    return send_from_directory(IMAGE_PATH+app_id,page_id+".png")